package com.yunxi.poc.starbucks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunxi.poc.starbucks.common.BaseController;
import com.yunxi.poc.starbucks.entity.TOrderNewDetails;
import com.yunxi.poc.starbucks.service.ITOrderNewDetailsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shiyuan.xlc
 * @since 2021-08-25
 */
@Controller
@RequestMapping("/starbucks/order_new_detail")
public class TOrderNewDetailsController extends BaseController {

    @Resource
    ITOrderNewDetailsService orderNewDetailsService;

    @ResponseBody
    @RequestMapping(value = {"/batch"}, method = RequestMethod.GET)
    public ResponseEntity batchGenDetails() {
        this.orderNewDetailsService.batchGenDetails();
        return ResponseEntity.status(HttpStatus.OK).body(1);
    }

    @ResponseBody
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public ResponseEntity pageOrderNewDetails(@RequestParam("id") String orderId) {
        if (!StringUtils.hasLength(orderId)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("0");
        }

        QueryWrapper<TOrderNewDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", orderId);
        List<TOrderNewDetails> orderNewDetailsList = orderNewDetailsService.list(queryWrapper);
        orderNewDetailsList.sort(Comparator.comparing(TOrderNewDetails::getRate));
        return ResponseEntity.status(HttpStatus.OK).body(orderNewDetailsList);
    }
}